define(['zepto', 'utils', 'JST'], function($, utils, JST){
  return {
    init: function(_CTX_, temp) {
      this._CTX_ = _CTX_
      this.temp = temp
      this.modal = {}
      
      this.$addressEdit = $('[href="#address-edit"]')
      this.$addressEdit.length ? this.bindPushAction() : void 0
    },

    bindPushAction: function() {

      var that = this

      // on address edit
      $(document).on('click', '[href="#address-edit"]', function(e) {
        e.preventDefault()
        var defer = that.compileaddressPage()

        $.when(defer).done(function($page) {
                $.push.transitionIn($page)
            })

      })

      // On address edited
      this.$addressEdit.on('edited:profile:address', function(e, $page, address) {

        var itemDomString = $(JST.addressItemTpl({'address': address}))

        if(that.temp) {
          $.toast("修改收货地址成功")
          setTimeout(function() {
                  window.location.reload()
                 }, 500)
        } else {
          $.push.transitionOut($page, {
            beforeTransition: function() {
              that.$addressEdit.off()
                .parent().empty().html(itemDomString)
            }
          })
        }
      })
    },

    compileaddressPage: function() {

      var that = this,
        deferPage = $.Deferred(),
        defer = this.requestaddress()

      $.when(defer).done(function(data) {

        var $page = $(JST.addressEditTpl({
          'address': data.address
        }))

        // Store request address
        that.modal.address = data.address

        // Bind submit
        that.bindaddressEditPageEvent($page)

        // Set defer as resolve status
        deferPage.resolve($page)
      })

      return deferPage.promise()
    },

    requestaddress: function() {
      var defer = $.Deferred(),
              url = this._CTX_.root + '/profile/address'

          // Show request indicator
          $.showIndicator()

          // Request for rounds data
          $.ajax({
            'type': 'GET',
            'url': url,
            'cache': false
          }).done(function(response) {
            $.hideIndicator()  // Hide request indicator
            utils.processResult(response, function(result) {
                defer.resolve(result.data)
            }, function(result) {
                $.toast(result.message || '获取收货地址失败')
            })
          })

        return defer.promise()
    },

    bindaddressEditPageEvent: function($page) {
      var that = this,
        $address = $('textarea[name="address"]', $page)

        $page.on('click', '#submit', function(e) {

          e.preventDefault()

          var address = $address.val().trim()
          if(!address) $.toast('收货地址不能为空')

          var submiting = that.submitaddress(address)
          $.when(submiting).done(function(data) {
            that.$addressEdit.trigger('edited:profile:address', [$page, that.address])
          })
        })
    },

    submitaddress: function(address) {
      var that = this, defer = $.Deferred()
      $.showIndicator()
      $.post(that._CTX_.root + "/profile/address", {
        'address': address,
        '_method': 'PATCH'
      }).done(function(response) {
        $.hideIndicator()
              utils.processResult(response, function(result) {
                defer.resolve(result.data)
            }, function(result) {
                $.toast(result.message || '修改失败')
                defer.resolve(result.data)
            })
      })
    }
  }
})